<?php

/**
 * Controller per inserimento Evento
 *
 * @author Gigi Pastore (gigi@la-fabbrica.org)
 * @created 16-mag-2014
 */
class InsertController extends Classes_Core_Controller
{

    public function init()
    {
        $this->model_instrument = new Classes_Instrument;
        $this->model_user = new Classes_Users;

	$this->days             = $this->getResource( 'Days' );
	$this->months           = $this->getResource( 'Months' );
        $this->layout->days     = $this->days;
        $this->layout->months   = $this->months;
        
	// Edit...
	if( is_numeric( $id = $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $instrument = $this->model_instrument->fetchById( array( $id ) );

	    if( empty( $instrument ) )
		throw new Classes_Core_Exceptions( 'Nessun id' );
	    
	    $this->instrument = array_shift( $instrument );
	    
	    // Se sto editando precompilo la sessione
	    if( !empty( $this->instrument ) AND empty( $_SESSION['Instrument'] ) AND ( $this->getAction() != 'riepilogo' ) )
	    {
		# Formattiamo campi
                list( $this->instrument['activity_start_date_d'], $this->activity['activity_start_date_m'], $this->activity['activity_start_date_y'] ) = explode( '-', $this->activity['activity_start_date_ita'] );
                list( $this->instrument['activity_end_date_d'], $this->activity['activity_end_date_m'], $this->activity['activity_end_date_y'] ) = explode( '-', $this->activity['activity_end_date_ita'] );
	    }
	}

        # Assegniamo i dati al layout
        $this->layout->data = $this->instrument;
        $this->layout->instrument_id = $this->instrument['instrument_id'];
        
        // Dati Form
        $this->form_data = $this->getParams();
        $this->layout->users = $this->model_user->fetchAll();
    }

    public function indexAction()
    {
	$this->layout->content = array( 'script' => 'insert/index' );
    }
    
    public function infoAction()
    {
	if( $this->form_data['conferma'] AND $this->form_data['conferma'] !== '' )
	{
	    $this->layout->data = $this->form_data;

            $start_date = $this->form_data['activity_start_date_y'] . '-' . $this->form_data['activity_start_date_m'] . '-' . $this->form_data['activity_start_date_d'];
            $end_date = $this->form_data['activity_end_date_y'] . '-' . $this->form_data['activity_end_date_m'] . '-' . $this->form_data['activity_end_date_d'];
            
            // Mapping...
            $data = array(
                'activity_id'           => $this->activity['activity_id'],
                'activity_title'        => $this->form_data['activity_title'],
                'activity_status'       => 1,
                'activity_description'  => $this->form_data['activity_description'],
                'activity_assignedto'   => $this->form_data['activity_assignedto'],
                'activity_start_date'   => $start_date == '--' ? null : $start_date,
                'activity_end_date'     => $end_date == '--' ? null : $end_date,
                'activity_start_time'   => $this->form_data['activity_start_time_h'] != '' ? $this->form_data['activity_start_time_h'] .':'. $this->form_data['activity_start_time_m'] : '',
                'activity_end_time'     => $this->form_data['activity_end_time_h'] != '' ? $this->form_data['activity_end_time_h'] .':'. $this->form_data['activity_end_time_m'] : ''
            );

            // Edit o Aggiunta?
            if( empty( $this->activity ) )
                $data['activity_createdby'] = $this->application->auth->user['user_id'];
            else
                $data['activity_modifiedby'] = $this->application->auth->user['user_id'];

            $model = new Classes_Activity( $data );

            if( is_numeric( $activity_id = $model->save()->activity_id ) )
            {
                // Redirect...
                $this->layout->forward = 'attivita/';
                return $this->layout->content = array( 'script' => 'completed' );
            }
            else
            {
                $errorMsg = $model->errors;

                if( $model->errors['activity_title'] )
                    $errorMsg['activity_title'] = sprintf( ERR_FORMAT, 'Titolo' );

                if( $model->errors['activity_start_date'] )
                    $errorMsg['activity_start_date'] = ERR_DATE;

                if(  $model->errors['activity_end_date'] )
                    $errorMsg['activity_end_date'] = 'La data di fine deve essere inferiore alla data d\'inizio';
            }          
	}

	# Assegniamo i dati al layout
	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'attivita/info' );        
    }
       
    public function vediAction()
    {
        if( !is_array( $this->activity ) )
            trigger_error( 'Errore Id Contatto' );
        
	# Assegniamo i dati al layout
	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'attivita/vedi' );        
    }

    public function agendaAction()
    {
	# Assegniamo i dati al layout
	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'attivita/agenda' );
    }
}